package com.zz.img.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zz.img.util.FileNameUtils;
import com.zz.img.util.FileUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

@RestController
@EnableAutoConfiguration
public class IndexController {

    @Resource
    private Environment env;

    final public static int CODE_OK=0;//正常状态
    final public static int CODE_ERROR=500;//系统错误

    @RequestMapping("/")
    public String index (){
        return "图片上传微服务";
    }

    @ResponseBody
    @RequestMapping("fileUpload")
    public String fileUpload(@RequestParam("file") MultipartFile[] files){

        JSONObject json= new JSONObject();

        // 要上传的目标文件存放路径
        String localPath =  env.getProperty("web.upload.path");
        String urlPath =  env.getProperty("web.urlPath");
        JSONArray paths= new JSONArray();
        for (int i = 0; i <files.length; i++) {
            MultipartFile file =files[i];
            //本地路径
            String fileName=FileNameUtils.getTimeFileName()+"/"+FileNameUtils.getFileName(file.getOriginalFilename());
            //返回访问路径
            String retfileName=urlPath+"/"+fileName;
            if (FileUtils.upload(file, localPath, fileName)){
                // 上传成功
                paths.add(retfileName);
            }
        }
        if(paths.size()>0){
            json.put("errno", CODE_OK);
            json.put("msg",  "上传成功！");
            json.put("data", paths);
        }else {
            json.put("errno", CODE_ERROR);
            json.put("msg", "上传失败！");
        }
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping("imgUploadBase64")
    public String fileUploadBase64(String base64){

        JSONObject json= new JSONObject();

        // 要上传的目标文件存放路径
        String localPath =  env.getProperty("web.upload.path");
        String urlPath =  env.getProperty("web.urlPath");
        JSONArray paths= new JSONArray();
        String postfix = "";
        String data1 = "";
        String [] d =base64.split(";base64,");
        if(d != null && d.length == 2){
            postfix = d[0];
            data1 = d[1];
        }
        byte[] bs = Base64Utils.decodeFromString(data1);

        InputStream in=new ByteArrayInputStream(bs);

        //本地路径
        String fileName=FileNameUtils.getTimeFileName()+"/"+FileNameUtils.getFileNameByFix("jpg");
        //返回访问路径
        String retfileName=urlPath+"/"+fileName;
        if (FileUtils.upload(in, localPath, fileName)){
            // 上传成功
            paths.add(retfileName);
        }
        if(paths.size()>0){
            json.put("errno", CODE_OK);
            json.put("msg",  "上传成功！");
            json.put("data", paths);
        }else {
            json.put("errno", CODE_ERROR);
            json.put("msg", "上传失败！");
        }
        return json.toJSONString();
    }
}
